#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QVBoxLayout>
#include <QFrame>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 初始化libVLC
    vlcInstance1 = libvlc_new(0, NULL);
    vlcMediaPlayer1 = libvlc_media_player_new(vlcInstance1);
    vlcMedia1 = NULL;

    vlcInstance2 = libvlc_new(0, NULL);
    vlcMediaPlayer2 = libvlc_media_player_new(vlcInstance2);
    vlcMedia2 = NULL;

    vlcInstance3 = libvlc_new(0, NULL);
    vlcMediaPlayer3 = libvlc_media_player_new(vlcInstance3);
    vlcMedia3 = NULL;

    vlcInstance4 = libvlc_new(0, NULL);
    vlcMediaPlayer4 = libvlc_media_player_new(vlcInstance4);
    vlcMedia4 = NULL;


    connect(ui->playButton, &QPushButton::clicked, this, &MainWindow::playRTMPStream);
    connect(ui->stopButton, &QPushButton::clicked, this, &MainWindow::stopPlayback);

}

MainWindow::~MainWindow()
{
    delete ui;
    //清理资源
    libvlc_media_player_stop(vlcMediaPlayer1);
    libvlc_media_player_release(vlcMediaPlayer1);
    libvlc_release(vlcInstance1);
    libvlc_media_player_stop(vlcMediaPlayer2);
    libvlc_media_player_release(vlcMediaPlayer2);
    libvlc_release(vlcInstance2);
    libvlc_media_player_stop(vlcMediaPlayer3);
    libvlc_media_player_release(vlcMediaPlayer3);
    libvlc_release(vlcInstance3);
    libvlc_media_player_stop(vlcMediaPlayer4);
    libvlc_media_player_release(vlcMediaPlayer4);
    libvlc_release(vlcInstance4);
}

void MainWindow::playRTMPStream()
{
    // RTMP流服务器URL
    const char *mediaUrl1 ="rtmp://10.7.178.63:9090/live";  //zzb
    const char *mediaUrl2 ="rtmp://10.7.178.220:9090/live"; //ywt
    const char *mediaUrl3 ="rtmp://10.7.178.63:9090/live"; //zzb
    const char *mediaUrl4 ="rtmp://10.7.178.189:9090/live"; //hzy
    if (vlcMedia1 != NULL)
    {
        libvlc_media_release(vlcMedia1);
    }
    if (vlcMedia2 != NULL)
    {
        libvlc_media_release(vlcMedia2);
    }
    if (vlcMedia3 != NULL)
    {
        libvlc_media_release(vlcMedia3);
    }
    if (vlcMedia4 != NULL)
    {
        libvlc_media_release(vlcMedia4);
    }
    vlcMedia1 = libvlc_media_new_location(vlcInstance1, mediaUrl1);
    libvlc_media_player_set_media(vlcMediaPlayer1, vlcMedia1);
    libvlc_media_player_set_xwindow(vlcMediaPlayer1, ui->frame->winId());
    libvlc_media_player_play(vlcMediaPlayer1);
    vlcMedia2 = libvlc_media_new_location(vlcInstance2, mediaUrl2);
    libvlc_media_player_set_media(vlcMediaPlayer2, vlcMedia2);
    libvlc_media_player_set_xwindow(vlcMediaPlayer2, ui->frame_2->winId());
    libvlc_media_player_play(vlcMediaPlayer2);
    vlcMedia3 = libvlc_media_new_location(vlcInstance3, mediaUrl3);
    libvlc_media_player_set_media(vlcMediaPlayer3, vlcMedia3);
    libvlc_media_player_set_xwindow(vlcMediaPlayer3, ui->frame_3->winId());
    libvlc_media_player_play(vlcMediaPlayer3);
    vlcMedia4 = libvlc_media_new_location(vlcInstance4, mediaUrl4);
    libvlc_media_player_set_media(vlcMediaPlayer4, vlcMedia4);
    libvlc_media_player_set_xwindow(vlcMediaPlayer4, ui->frame_4->winId());
    libvlc_media_player_play(vlcMediaPlayer4);
}

void MainWindow::stopPlayback()
{
    libvlc_media_player_stop(vlcMediaPlayer1);
    libvlc_media_player_stop(vlcMediaPlayer2);
    libvlc_media_player_stop(vlcMediaPlayer3);
    libvlc_media_player_stop(vlcMediaPlayer4);
}
